/*
 * NetworkUtilities.java
 *
 * Created on 31 de enero de 2007, 10:47 AM
 */

package networkFlow;

/**
 *
 * @author tavobula
 */
public class NetworkUtilities {
    
    /** Creates a new instance of NetworkUtilities */
    public NetworkUtilities() {
    }
    
    public static int flow(Network G, int v){
        int x=0;
        AdjList A =G.getAdjList(v);
        for(Edge e=A.beg();!A.end(); e=A.nxt()){
            x += e.from(v) ? e.getPflow() : -e.getPflow();
        }
        return x;
    }
    
    public static boolean flowcheck(Network G, int s, int t){
        for(int v=0; v<G.getV();++v)
            if((v !=s)&&(v!=t))
                if(flow(G,v) != 0) return false;
            int sflow = flow(G,s);
            if(sflow<0) return false;
            if(sflow + flow(G,t)!=0) return false;
            return true;
        
    }
    
    public static int cost(Network G){
        int x=0;
        for(int v=0; v<G.getV();++v){
            AdjList A = G.getAdjList(v);
            for (Edge e=A.beg();!A.end(); e=A.nxt())
                if(e.from(v) && e.costRto(e.getW())<Edge.C)
                    x+=e.getPflow()*e.costRto(e.getW());
        }
        return x;
    }
    
}
